﻿#ifndef WIDGETCOMSERVER_H
#define WIDGETCOMSERVER_H

#include <QWidget>
#include <QGroupBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QComboBox>
#include <QHBoxLayout>
#include "TcpServer.h"

class WidgetComServer : public QGroupBox
{
    Q_OBJECT
public:
    explicit WidgetComServer(const QString& name = "", QWidget *parent = nullptr);
    void addComServer(TcpServer* client);

signals:
    void sigConfigChanged(const TcpComConfig& config);
    void sigAddOne(const TcpComConfig& config);
    void sigDeleteOne(const QString& name);
    void sigWriteData(const QString& txt);
private:
    void setupUI();
    void setupConfigUI();
    void setupSendRev();
    void initConnection();

    TcpComConfig getConfig();

    void onSaveConfig();

    void onComSelectChanged(const int& index);
private:
    QMap<QString, TcpServer*> mMapComServer;

    //control
    QHBoxLayout* mLayout;
    //PLC配置控件
    QPushButton* pbtnAddOne;
    QPushButton* pbtnDeleteOne;
    QPushButton* pbtnApplyPLC;
    QComboBox* cbboxComNameList;

    //socket
    QWidget* wgtSocketConfig;
    QLineEdit* ledtOpponentIP;
    QSpinBox* spboxOpponentPort;
    QLineEdit* ledtLocalIP;
    QSpinBox* spboxLocalPort;
    QCheckBox* chkboxSendHex;

    //接收区
    QCheckBox* chkboxIsDisplayData;
    QCheckBox* chkboxIsDisplayHex;
    QPushButton* pbtnClear;
    QTextEdit* txtedtDataRev;

    //发送区
    QTextEdit* txtedtDataSend;
    QPushButton* pbtnSendData;
};

#endif // WIDGETCOMSERVER_H
